<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
            list-style: none;
        }
        .menu {
            width: 150px;
            margin: 40px;
            line-height: 30px;
        }
        p{
            border: 1px solid #000;
            padding-left: 10px;
        }
        ul{
            display: none;
        }
        li{
            border: 1px solid #000;
            padding-left: 10px;
        }
    </style>
</head>
<body>

    <div class="menu">
        <p>请选择</p>
        <ul>
            <li>吃饭</li>
            <li>睡觉</li>
            <li>撸代码</li>
        </ul>
    </div>


    <script>

        // 事件冒泡    子元素的同类型事件会传播到父元素
        //   阻止事件冒泡   e.stopPropagation()

        var oMenu = document.querySelector('.menu') ;
        var oP = oMenu.querySelector('p') ;
        var oUl = oMenu.querySelector('ul') ;
        var oLis = oMenu.querySelectorAll('li') ;

        oP.addEventListener('click' , function(e) {
            e.stopPropagation();
            // 显示ul
            oUl.style.display = 'block' ;
            
        })


        // 给li绑定事件
        oLis.forEach(function(v){
            v.addEventListener('click' , function() {
                oP.innerHTML = this.innerHTML;
                oUl.style.display = 'none'
            })
        })


        document.addEventListener('click' , function() {
            oUl.style.display = 'none'
        })

    </script>
    
</body>
</html>